using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Entities;
using System.Configuration;
namespace DAL
{
    public class tbNhanVienDAL
    {
        private SqlConnection cn = null;
        public tbNhanVienDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);           
            cn.Open();
        }
        public void Insert(tbNhanVien nv)
        {
            SqlCommand cm = new SqlCommand("sp_tbNhanVien_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenNV", nv.TenNV);
            cm.Parameters.AddWithValue("@DiaChi", nv.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", nv.DienThoai);
            cm.Parameters.AddWithValue("@Email", nv.Email);
            cm.ExecuteNonQuery();

        }
        public void Update(tbNhanVien nv)
        {
            SqlCommand cm = new SqlCommand("NhanVien_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenNV", nv.TenNV);
            cm.Parameters.AddWithValue("@DiaChi", nv.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", nv.DienThoai);
            cm.Parameters.AddWithValue("@Email", nv.Email);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("NhanVien_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IDNhanVien", Id);
            cm.ExecuteNonQuery();
        }
        public Entities.tbNhanVien.tbNhanVienCollection Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("NhanVien_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbNhanVien.tbNhanVienCollection nvcollection = new tbNhanVien.tbNhanVienCollection();
            while (dr.Read())
            {
                tbNhanVien nv = new tbNhanVien();
                nv.IDNhanVien = int.Parse (dr.GetValue(0).ToString());
                nv.TenNV = dr.GetValue(1).ToString();
                nv.DiaChi = dr.GetValue(2).ToString();
                nv.DienThoai = int.Parse(dr.GetValue(3).ToString());
                nv.Email = dr.GetValue(4).ToString();

                nvcollection.Add(nv);              
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return nvcollection ;
        }
    }
}
